<?php
namespace app\index\controller;
use \think\Controller;
use \think\Db;
use app\index\model\VerCode;
use \think\Loader;
use \think\File;
use app\functions;
class Read extends Controller
{   
    // 从数据库中取出当前用户资料并加载模版
    public function index()
    {
        $ques = (new VerCode)->getRandOne();
    	$pid = input('pid');
        $uid = session('uid') ? session('uid') : '';
    	 
        Db::table('post')
        ->where('pid',$pid)
        ->setInc('view_num');

        $data=Db::table('post')
        ->alias('p')
        ->join('user u','p.uid = u.uid')
        ->join('cate c','p.class_id = c.cid')
        ->field('post.*,cate.cname,user.avatar,user.username,user.level,user.is_approve')
        ->find($pid);
// var_dump($data);exit;
// 
        $reply=Db::table('reply')
        ->alias('r')
        ->join('user u','r.uid = u.uid')
        ->join('good g','g.rid = r.rid and g.uid="'.$uid.'"','left')
        ->field('r.*,user.avatar,user.username,user.level,user.is_approve,g.gid')
        ->where('pid',$pid)
        ->select();
        $data['ctime']=ftime($data['ctime']);
        // var_dump($reply);exit;
        return $this->fetch('',['data'=>$data,'ques'=>$ques,'reply'=>$reply]);

    }
    // 回复
    public function reply(){
        $data = input('post.');

        if ($data['vercode']<>session('answer')) {
            return ['error'=>0,'info'=>'验证码不正确,请重新输入'];
        }

        // $data = ['class_id'=>$data['class_id'],'title'=>$data['title'],'content'=>$data['content'],'kiss'=>$data['kiss'],'uid'=>session('uid'),'ctime'=>time()];
        $data['ctime'] = time();
        $data['uid'] = session('uid');
        $res = Db::table('reply')->insert($data);
        Db::table('post')->where('pid',$data['pid'])->setInc('answer_num');
        if($res){
            return ['error'=>0,'info'=>'发表成功','url'=>url('index/Read/index',['pid'=>$data['pid']])];
        }else{
            return ['error'=>1,'info'=>'发表失败'];
        }

    }
    //点赞
    public function good(){
        $data = input('post.');
        $res = Db::table('good')->where($data)->find();
        if($res){
            Db::table('good')->where($data)->delete();
            Db::table('reply')->where('rid',$data['rid'])->setDec('zan_num');
            return ['error'=>1,'info'=>'取消点赞'];
        }else{
            $data['ctime'] = time();
            Db::table('good')->insert($data);
            Db::table('reply')->where('rid',$data['rid'])->setInc('zan_num');
            return ['error'=>0,'info'=>'点赞成功'];
        }
    }

    //采纳
    public function accept(){
        $data = input('post.');
        $repalyData = Db::table('reply')->where('rid',$data['rid'])->find();
        $postData = Db::table('post')->where('pid',$data['pid'])->find();
        Db::table('reply')->where('rid',$data['rid'])->update(['status'=>'1']);
        Db::table('post')->where('pid',$data['pid'])->update(['status'=>'1']);
        Db::table('user')->where('uid',$repalyData['uid'])->setInc('kiss',$postData['kiss']);
        return['error'=>0,'info'=>'采纳成功','url'=>url('index/Read/index',['pid'=>$data['pid']])];
    }
 }
?>